import PyQt5
from PyQt5 import QtCore
from PyQt5 import QtBluetooth
from PyQt5.QtBluetooth import QBluetoothDeviceInfo


class DeviceFinder(QtCore.QObject):
    def __init__(self):
        super().__init__()
        self.m_devices = []

        self.deviceDiscoveryAgent = QtBluetooth.QBluetoothDeviceDiscoveryAgent(self)
        self.deviceDiscoveryAgent.setLowEnergyDiscoveryTimeout(1500)
        self.deviceDiscoveryAgent.deviceDiscovered.connect(self.add_device)
        self.deviceDiscoveryAgent.error.connect(self.scan_error)
        self.deviceDiscoveryAgent.finished.connect(self.scan_finished)
        self.deviceDiscoveryAgent.canceled.connect(self.scan_finished)

        self.deviceDiscoveryAgent.start(QtBluetooth.QBluetoothDeviceDiscoveryAgent.DiscoveryMethod(2))

    def add_device(self, device: QBluetoothDeviceInfo):
        # If device is LowEnergy-device, add it to the list
        if device.coreConfigurations() and QtBluetooth.QBluetoothDeviceInfo.LowEnergyCoreConfiguration:
            self.m_devices.append(QtBluetooth.QBluetoothDeviceInfo(device))
            print("Low Energy device found. Scanning more...")

    def scan_finished(self):
        print("scan finished")
        for i in self.m_devices:
            # QtBluetooth.QBluetoothDeviceInfo.
            print('UUID: {UUID}, Name: {name}, rssi: {rssi}, Address: {address}'.format(UUID=i.deviceUuid().toString(),
                                                                                        name=i.name(),
                                                                                        rssi=i.rssi(),
                                                                                        address=i.address().toString()))
        self.quit()

    def scan_error(self):
        print("scan error")

    def quit(self):
        print("Bye!")
        QtCore.QCoreApplication.instance().quit()


if __name__ == "__main__":
    import sys

    app = QtCore.QCoreApplication(sys.argv)
    hello = DeviceFinder()
    sys.exit(app.exec_())
